function loadCompany()
{
	$.post("propertyCompanyAction!getPropertyInfo.action", {}
	, function(data)
	{
		data = eval("(" + data + ")");
		
		if (data.found == 0)
		{
			alert("未找到相关数据");
		}
		else
		{
			var option = '<option value="-1">请选择</option>';
			
			$(data.data).each
			(
					function(ix, ite) 
					{
						option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						
					});
			
			$("#property_company").empty().append(option);
			
		}
	});
}

function loadCommunity(companyId)
{
	$.post("communityAction!getCommunityInfo.action", {inParma:JSON.stringify({"companyId":companyId})}
	, function(data)
	{
		data = eval("(" + data + ")");

		if (data.found == 0)
		{
			alert("未找到相关数据");
		}
		else
		{
			var option = '<option value="-1">请选择</option>';
			
			$(data.data).each
			(
					function(ix, ite) 
					{
						option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						
					});
			
			$("#community").empty().append(option);
			
		}
	});
}

function loadBuilding(communityId)
{
	$.post("buildingAction!getBuildingInfo.action", {inParma:JSON.stringify({"communityId":communityId})}
	, function(data)
	{
		data = eval("(" + data + ")");

		if (data.found == 0)
		{
			alert("未找到相关数据");
		}
		else
		{
			var option = '<option value="-1">请选择</option>';
			
			$(data.data).each
			(
					function(ix, ite) 
					{
						option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						
					});
			
			$("#building").empty().append(option);
			
		}
	});
}

function loadUnit(ownerBuildingId)
{
	$.post("buildingAction!getUnitInfo.action", {inParma:JSON.stringify({"ownerBuildingId":ownerBuildingId})}
	, function(data)
	{
		data = eval("(" + data + ")");

		if (data.found == 0)
		{
			alert("未找到相关数据");
		}
		else
		{
			var option = '<option value="-1">请选择</option>';
			
			$(data.data).each
			(
					function(ix, ite) 
					{
						option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						
					});
			
			$("#unit").empty().append(option);
			
		}
	});
}

function loadRooms(unitId)
{
	$.post("roomAction!getRoomInfo.action", {inParma:JSON.stringify({"unitId":unitId})}
	, function(data)
	{
		data = eval("(" + data + ")");

		if (data.found == 0)
		{
			alert("未找到相关数据");
		}
		else
		{
			var option = '<option value="-1">请选择</option>';
			
			$(data.data).each
			(
					function(ix, ite) 
					{
						option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						
					});
			
			$("#room").empty().append(option);
			
		}
	});
}

function loadParkingTypes(communityId)
{
	$.post("parkingTypeAction!getParkingTypes.action", {inParma:JSON.stringify({"communityId":communityId})}
	, function(data)
	{
		data = eval("(" + data + ")");

		if (data.found == 0)
		{
			alert("未找到相关数据");
		}
		else
		{
			var option = '';
			
			$(data.data).each
			(
					function(ix, ite) 
					{
						option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						
					});
			
			$("#parking_type").empty().append(option);
			
		}
	});
}

function loadVehicle(roomId)
{
	$.post("vehicleAction!getVehicleInfo.action", {inParma:JSON.stringify({"roomId":roomId})}
	, function(data)
	{
		data = eval("(" + data + ")");

		if (data.found == 0)
		{
			alert("未找到相关数据");
		}
		else
		{
			var option = '';
			
			$(data.data).each
			(
					function(ix, ite) 
					{
						option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						
					});
			
			$("#plate_nbr").empty().append(option);
			
		}
	});
}

$(document).ready(function()
{
	$("#property_company").bind("change", function(){
		param = $(this).val();
		
		if (param != '-1')
		{
			loadCommunity(param);
		}
		else
		{
			$("#community").empty();
		}
		
		$("#building").empty();
		$("#unit").empty();
		$("#room").empty();
		$("#parking_type").empty();
		$("#plate_nbr").empty();
	});
	
	$("#community").bind("change", function(){
		param = $(this).val();
		
		if (param != '-1')
		{
			loadBuilding(param);
			loadParkingTypes(param);
		}
		else
		{
			$("#building").empty();
			$("#parking_type").empty();
		}
		
		$("#unit").empty();
		$("#room").empty();
		$("#plate_nbr").empty();
		
	});
	
	$("#building").bind("change", function(){
		param = $(this).val();
		
		if (param != '-1')
		{
			loadUnit(param);
		}
		else
		{
			$("#unit").empty();
		}
		
		$("#room").empty();
		$("#plate_nbr").empty();
	});
	
	$("#unit").bind("change", function(){
		param = $(this).val();
		
		if (param != '-1')
		{
			loadRooms(param);
		}
		else
		{
			$("#room").empty();
		}
		
		$("#plate_nbr").empty();
		
	});
	
	$("#room").bind("change", function(){
		param = $(this).val();
		if (param != '-1')
		{
			loadVehicle(param);
		}
		else
		{
			$("#plate_nbr").empty();
		}
		
	});	
	
});

function saveParking()
{
	var parking_nbr = $("#parking_nbr").val();
	
	if (parking_nbr == null || parking_nbr == "null" || parking_nbr == "")
	{
		$.sucmodal("提示", "车位编码不能为空");
		
		return;
	}
	
	var communityId = $("#community").val();
//	alert("communityId-->"+communityId);
	
	if (communityId == null || communityId == "null" || communityId == "")
	{
		$.sucmodal("提示", "小区不能为空");
		
		return;
	}

	var roomId = $("#room").val();
	var parkingTypeId = $("#parking_type option:selected").val();
//	alert("parkingType-->"+parkingType);
	if (parkingTypeId == null || parkingTypeId == "null" || parkingTypeId == "")
	{
		$.sucmodal("提示", "车位类型不能为空");
		
		return;
	}
	
	var parkingPosition = $("#parking_position").val();
	var vehicleId = $("#plate_nbr").val();
	var jsarray = new Array();
	var parking = {};
	
	parking.parkingNbr = parking_nbr;
	parking.communityId = communityId;
	parking.parkingTypeId = parkingTypeId;
	parking.parkingPosition = parkingPosition;

	if (vehicleId != null && vehicleId != "" && vehicleId != "null")
	{
		if (roomId == null || roomId == "null" || roomId == "")
		{
			$.sucmodal("提示", "请选择车辆对应房间");
			
			return;
		}
		else
		{
			parking.vehicleId = vehicleId;
			parking.roomId = roomId;
			
			jsarray.push(parking);
			
		}

	}
	else
	{
		if (confirm("未关联车牌号，继续提交吗？"))
		{
			jsarray.push(parking);
			
		}
		else
		{
			return;
		}
	}
	
	commit(jsarray);
	
}

function commit(inJsonArray)
{
	var inparam = {data:{}};
	inparam.data = inJsonArray;
	
	$.post("parkingAction!saveParking.action", 
			{inParam : JSON.stringify(inparam)}, 
			function(data) 
			{
				data = eval("(" + data + ")");
				
				if (data.ok == "1") 
				{
					$.sucmodal("提示", "操作成功");
				}
				else 
				{
					alert("保存失败");
				}
	});
}

